package com.example.netty.common;

import com.example.netty.vo.FaFbVo;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FieldClass {

    public static <T> T fieldVoluation(Class<T> clazz, String[] strings, Integer len) throws Exception {
        T instance = clazz.getDeclaredConstructor().newInstance();
        Field[] ListField = clazz.getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
        for (int i = 0; i < len; i++) {
            fields[i].setAccessible(true);
            if (fields[i].getType() == BigDecimal.class) {
                if (fields[i].isAnnotationPresent(Reserve.class)) {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf(strings[i]), fields[i].getAnnotation(Reserve.class).value()));
                } else {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf(strings[i])));
                }
            } else if (fields[i].getType() == Integer.class) {
                fields[i].set(instance, Integer.valueOf(strings[i]));
            } else if (fields[i].getType() == String.class) {
                fields[i].set(instance, strings[i]);
            }
        }
        // 方法体逻辑
        return instance;
    }

    public static <T, F> void fieldCompare(Class<T> clazz, T a, F b, Map<String, Object> fmap) throws Exception {
        if (b == null && a!=null) {
            Class<F> bzClass = (Class<F>) a.getClass();
            b = bzClass.getDeclaredConstructor().newInstance();
        }

        Map<String, FaFbVo> map = new HashMap<>();
        Field[] ListField = clazz.getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
        String tableName = clazz.getSimpleName().replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
        if (a == null) {
            fmap.put(tableName, null);
            return;
        }
        boolean bool=true;
        if(clazz.isAnnotationPresent(Invalid.class)){
            String name = clazz.getAnnotation(Invalid.class).name();
            Integer value = clazz.getAnnotation(Invalid.class).value();
            Field field = a.getClass().getDeclaredField(name);
            field.setAccessible(true);
//            System.out.println(name+"============>");
//            System.out.println(value);
//            System.out.println(field.get(a));
            if(Double.valueOf(String.valueOf(field.get(a)))<value) bool=false;

        }
        for (Field FieldA : fields) {
            String name = FieldA.getName();
            Field FieldB = b.getClass().getDeclaredField(name);
            FieldA.setAccessible(true);
            FieldB.setAccessible(true);
            FaFbVo faFbVo = new FaFbVo();
            faFbVo.setColumnname(name);
            faFbVo.setCurrentValue(FieldA.get(a));
            faFbVo.setWarningValue(FieldB.get(b));
            if (FieldA.isAnnotationPresent(Compare.class)) {
                String value = FieldA.getAnnotation(Compare.class).value();
                faFbVo.setSymbol(value);
                BigDecimal va = BigDecimal.valueOf(Double.valueOf((String) FieldA.get(a))) ;
                BigDecimal vb = BigDecimal.valueOf(Double.valueOf((String) FieldB.get(b))) ;
               if(bool){
                   switch (value) {
                       case ">":
                           if (va.compareTo(vb) > 0) {
                               faFbVo.setState(1);
                           }
                           break;
                       case "<":
                           if (va.compareTo(vb) < 0) {
                               faFbVo.setState(1);
                           }
                           break;
                       case "≥":

                           break;
                       case "≤":

                           break;
                   }
               }
            }
            map.put(name, faFbVo);
        }
        fmap.put(tableName, map);

    }

//    public static <T> void fieldGroup(Class<T> clazz, T a,Map<String, Object> gmap) throws Exception {
//        Map<String, List<Object>> groupedFields = new HashMap<>();
//        Field[] ListField = clazz.getDeclaredFields();
//        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
//        String tableName = clazz.getSimpleName().replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
//        for (Field FieldA : fields) {
//            String name=FieldA
//            if (FieldA.isAnnotationPresent(Group.class)){
//                String groupName =FieldA.getAnnotation(Group.class).value();
//                if(!groupedFields.containsKey(groupName )){
//                    groupedFields.put(groupName ,new ArrayList<>());
//                }
//                groupedFields.get(groupName ).add(field);
//            }else {
//
//            }
//        }
//
//    }


    public static void main(String[] args) throws Exception {


//        boolean bool = LeftMain.class.isAnnotationPresent(Invalid.class);
//        System.out.println(bool);
//        LeftMain.class

//        LeftMain leftMain = new LeftMain();
//        leftMain.setZjzs(BigDecimal.valueOf(13));
//        leftMain.setZwzzs(BigDecimal.valueOf(3));
//        LeftMainWarning leftMainWarning = new LeftMainWarning();
//        leftMainWarning.setZjzs(BigDecimal.valueOf(12));
//        leftMainWarning.setZwzzs(BigDecimal.valueOf(4));
//        List<Offside> OffsideList = null;
//        FieldClass.fieldCompare(LeftMain.class, leftMain, leftMainWarning, OffsideList, 11);

    }
}
